package cn.yunhe.controller;

import cn.yunhe.entity.Users;
import cn.yunhe.services.IUsersServices;
import cn.yunhe.services.impl.UsersServices;
import cn.yunhe.utils.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import lombok.Data;

@Data
public class UserAction extends BaseAction implements ModelDriven {
    private Users users;
    IUsersServices iUsersServices = new UsersServices();

    /**
     * 用户注册
     *
     * @return
     */
    public String reg() {
        System.out.println("userReg");
        if (users.getUsername() != null && users.getUsername() != "" && users.getPassword() != null && users.getPassword() != "") {
            int row = iUsersServices.userRegister(users);
            System.out.println(row);
            return SUCCESS;
        } else {
            getMapRequest().put("msg", "注册失败,请重新<a href='login.htm'>注册</a>");
            return ERROR;
        }
    }

    /**
     * 用户登录
     *
     * @return
     */
    public String login() {
        System.out.println("userLogin");
        if (users.getUsername() != null && users.getUsername() != "" && users.getPassword() != null && users.getPassword() != "") {
            iUsersServices.userLogin(users);

            return SUCCESS;
        } else {
            getMapRequest().put("msg", "登陆失败,请重新<a href='login.htm'>登陆</a>");
            return ERROR;
        }
    }

    /**
     * 检查用户
     *
     * @return
     */
    public String check() {
        System.out.println("userCheck");
        iUsersServices.userCheck(users);
        return SUCCESS;
    }

    @Override
    public Object getModel() {
        if (users == null) {
            users = new Users();
        }
        return users;
    }
}
